package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopBanner;
import cn.regexp.domain.ShopGoods;
import cn.regexp.query.BannerQuery;
import cn.regexp.service.IBannerService;
import cn.regexp.service.IGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * 轮播图控制器
 */
@Slf4j
@Controller
@RequestMapping("business/banners")
public class BannerController {
    //模板前缀
    private static final String prefix = "business/banners/";

    @Autowired
    private IBannerService bannersService;
    @Autowired
    private IGoodsService goodsService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:banners:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:banners:add")
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", ShopGoods.DELETE_NO);
        List<ShopGoods> contents = goodsService.list(wrapper);
        model.addAttribute("contents", contents);
        return prefix + "add";
    }

    @RequiresPermissions("business:banners:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", ShopGoods.DELETE_NO);
        List<ShopGoods> contents = goodsService.list(wrapper);
        model.addAttribute("contents", contents);
        model.addAttribute("banner", bannersService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:banners:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopBanner> query(BannerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopBanner> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(qo.getKeyword()), "title", qo.getKeyword())
                .eq(ObjectUtil.isNotEmpty(qo.getState()), "state", qo.getState())
                .eq(ObjectUtil.isNotEmpty(qo.getType()), "type", qo.getType());
        return new TablePageInfo<>(bannersService.list(wrapper));
    }


    //新增
    @RequiresPermissions("business:banners:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopBanner banner) {
        Long contentId = banner.getContentId();
        if (ObjectUtil.isNotEmpty(contentId)) {
            ShopGoods shopGoods = goodsService.getGoodsById(contentId);
            banner.setContentTitle(shopGoods.getGoodsName());
            banner.setContentUrl("/pages/goods_detail/index?goodsId=" + contentId);
        }
        bannersService.save(banner);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:banners:edit")
    @RequiresRoles("common")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopBanner banner) {
        Long contentId = banner.getContentId();
        if (ObjectUtil.isNotEmpty(contentId)) {
            ShopGoods shopGoods = goodsService.getGoodsById(contentId);
            banner.setContentTitle(shopGoods.getGoodsName());
            banner.setContentUrl("/pages/goods_detail/index?goodsId=" + contentId);
        }
        bannersService.updateById(banner);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:banners:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        bannersService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }
}
